<body class="">
<form id="questionEdit" class="layui-form " style="margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">科目类型</label>
        <div class="layui-input-inline">
            <select id="subjid" name="subjid" >
                <option value="">--选择类型--</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">考题类型</label>
        <div class="layui-input-inline">
            <select id="typeid" name="typeid" lay-filter="questionType" >
                <option value="">--选择类型--</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">阶段</label>
        <div class="layui-input-inline">
            <select id="questionStage" name="stage" lay-filter="questionType" >
                <option value="">--选择类型--</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-block">
            <textarea id="qtitle" name="qtitle" class="layui-textarea"  ></textarea>
        </div>
    </div>
    <div class="layui-form-item" id="optionA-div" hidden>
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <input id="optioA" name="optioA" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item" id="optionB-div" hidden>
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <input id="optioB" name="optioB" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item" id="optionC-div" hidden>
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <input id="optioC" name="optioC" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item" id="optionD-div" hidden>
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <input id="optioD" name="optioD" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">答案</label>
        <div class="layui-input-block">
            <textarea id="answer" name="answer" class="layui-textarea"  ></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">解题思路</label>
        <div class="layui-input-block">
            <textarea id="notes" name="notes" class="layui-textarea"  ></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <div class="layui-input-inline" >
                <input type="radio"   name="status" class="layui-input"  value="1" title="使用">
                <input type="radio"   name="status" class="layui-input" value="2" title="禁用" >
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            <button class="layui-btn layui-btn-primary" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
</body>
<script>
    layui.use(['form', 'config', 'admin','layer'], function () {
        var admin=layui.admin;
        var form=layui.form;
        var layer=layui.layer;
        $("#questionEdit").animate({"scrollTop":top})

        let tempData = admin.getTempData("question_data");
        $("#qtitle").val(tempData.qtitle);
        showOption(tempData);
        $("#answer").val(tempData.answer);
        $("#notes").val(tempData.notes);
        $("input[name='status'][value='" + tempData.status + "']").attr("checked", "checked");
        //获取考题类型
        admin.req("api-exam/questionType/findAll?status=1",{},function (res) {
            var data=res.data;
            for (let i = 0; i < data.length; i++) {
                $("#typeid").append('<option value="'+data[i].typeid+'">'+data[i].tpname+'</option>')
            }
            $("#typeid option[value='"+tempData.typeid+"']").attr("selected","selected");
        },"get");

        //获取科目类型
        admin.req("api-exam/questionSubject/findAll?status=1",{},function (res) {
            var data=res.data;
            for (let i = 0; i < data.length; i++) {
                $("#subjid").append('<option value="'+data[i].subjid+'">'+data[i].sjname+'</option>')
            }
            $("#subjid option[value='"+tempData.subjid+"']").attr("selected","selected");
        },"get");
        //获取考题阶段
        admin.req("api-exam/question/getStage",{},function (data) {
            for (let i = 0; i < data.length; i++) {
                $("#questionStage").append('<option value="'+data[i]+'">'+data[i]+'</option>')
            }
            $("#questionStage option[value='"+tempData.stage+"']").attr("selected","selected");
        },"get");
        form.render();

        //提交表单
        form.on("submit(saveBtn)",function (data) {
            data.field.qoptid=tempData.qoptid;
            data.field.quesid=tempData.quesid;
            admin.req("api-exam/question/edit",JSON.stringify(data.field),function (res) {
                layer.msg(res.msg);
            },"POST");
        });
        //选择题显示
        form.on("select(questionType)",function () {
            let data=tempData;
            data.typeid=$("#typeid").val();
           showOption(data);
        })
    });
    function showOption(data) {
        if(data.typeid=='4'){
            $("#optionA-div").attr("hidden",false);
            $("#optionB-div").attr("hidden",false);
            $("#optionC-div").attr("hidden",false);
            $("#optionD-div").attr("hidden",false);
            //赋值
            $("#optioA").val(data.optioA);
            $("#optioB").val(data.optioB);
            $("#optioC").val(data.optioC);
            $("#optioD").val(data.optioD);
        }else {
            $("#optionA-div").attr("hidden",true);
            $("#optionB-div").attr("hidden",true);
            $("#optionC-div").attr("hidden",true);
            $("#optionD-div").attr("hidden",true);
            $("#optioA").val("");
            $("#optioB").val("");
            $("#optioC").val("");
            $("#optioD").val("");
        }
    }
</script>